package com.dommy.tab.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.dommy.tab.R;
import com.dommy.tab.adapter.MsgContentFragmentAdapter;
import com.dommy.tab.pojo.WaterVO;
import com.dommy.tab.util.MyHttpObject;
import com.dommy.tab.util.MyUrlUtil;
import com.dommy.tab.util.UserUtil;
import com.dommy.tab.util.WaterUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 主页面
 */
public class MsgFragment extends Fragment {
    @BindView(R.id.tab_layout)
    TabLayout tabLayout;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    @BindView(R.id.water_one_Water)
    TextView water_one_Water;
    @BindView(R.id.water_one_Time)
    TextView water_one_Time;
    @BindView(R.id.water_one_Consumer)
    TextView water_one_Consumer;
    private MsgContentFragmentAdapter adapter;
    private List<String> names;
    MyHttpObject myHttpObject = new MyHttpObject();
    public List<WaterVO> list;
    public static boolean FLAG = true;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
        initData();
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_msg, container, false);
        ButterKnife.bind(this, view);

        adapter = new MsgContentFragmentAdapter(getChildFragmentManager());
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);

        // 更新适配器数据
        adapter.setList(names);
        return view;
    }

    private void initData() {
        names = new ArrayList<>();
        names.add("关注");
    }


    public void init(){

        final Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
//                System.out.println("WaterUtil.WATER_LIST               :   "+WaterUtil.WATER_LIST);
                while (WaterUtil.WATER_LIST == null || WaterUtil.WATER_LIST.size() ==0){
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                list = WaterUtil.WATER_LIST.get(0);
                handler.sendEmptyMessageDelayed(1, 100);

            }
        });
        thread.start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                while(MsgFragment.FLAG){
                    thread.run();
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
    public  Handler handler = new  Handler() {
        @Override
        public void handleMessage(Message msg) {
            if(msg.what == 1){
                setView();
            }
        }
    };
    public  void setView(){
        if( list == null || list.size() == 0){

        }else {
            water_one_Water.setText(list.get(0).getMonthWater()+"");
            water_one_Time.setText(list.get(0).getDateTime());
            water_one_Consumer.setText(list.get(0).getMonthConsumer()+"");
        }
    }

    @Override
    public void onResume() {
        setView();
        super.onResume();
    }

}
